<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/4/16
 * Time: 16:08
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\middleware;

use app\common\lib\Show;
use app\common\lib\Status;
use Htlove\Jwt;

class Auth
{
    /**
     * @param $request
     * @param \Closure $next
     * @return array|mixed|\think\response\Json
     */
    public function handle($request, \Closure $next)
    {
        $pathinfo = $request->pathinfo();
        if (preg_match('/login/', $pathinfo)) {
            return $next($request);
        }
        if (preg_match('/index/', $pathinfo)) {
            return $next($request);
        }
        //验证
        $access_token = $request->header('access_token');
        if(empty($access_token)){
            return Show::json_show(Status::mini_noLogin,"请先登录");
        }
        $check = Jwt::getInstance()->verifyToken($access_token);
        
        if($check === false){
            return Show::json_show(Status::mini_error,"验证失败");
        }
        $request->openid = $check['sub']['openid'];
//        $request->shop_id = $check['sub']['shop_id'];

        if (!preg_match('/user/', $pathinfo)) {
            if(empty($check['sub']['nickName'])){
                return Show::json_show(Status::mini_noLogin,"请先登录");
            }
        }

        return $next($request);
    }
}